package com.miaoshare.core.utils.oss;

import com.miaoshare.core.utils.LogUtil;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by zxs
 * on 2022/2/15
 */
public class SimpleCall {

    private static final boolean DEBUG = true;

    OkHttpClient okHttpClient;

    public SimpleCall() {
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
    }

    public void call(String method, Map<String, String> params) {
        call(method, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                try {
                    if (DEBUG) {
                        LogUtil.d("--------------------[onFailure] method = %1$s" + call.request().url());
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String json = response.body().string();
                    if (DEBUG) {
                        LogUtil.d("--------------------[onSuccess] method = %1$s, json = %2$s" +
                                call.request().url() + json);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void call(String method, Map<String, String> params, Callback callback) {
        if (DEBUG) {
            LogUtil.d("--------------------[call] method = %1$s%2$s, params = %3$s" +
                    method + params.toString());
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : params.keySet()) {
            builder.add(key, params.get(key));
        }
        FormBody body = builder.build();
        Request request = new Request.Builder()
                .url(UploadFileUtil.httpName + method)
                .post(body)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    public Response callUrl(String url, Map<String, String> params) throws Exception {
        if (DEBUG) {
            LogUtil.d("--------------------[call] url = %s, params = %s" + url + params.toString());
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : params.keySet()) {
            builder.add(key, params.get(key));
        }
        FormBody body = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        return okHttpClient.newCall(request).execute();
    }

    public void callUrl(String url, Map<String, String> params, Callback callback) {
        if (DEBUG) {
            LogUtil.d("--------------------[call] url = %s, params = %s" + url + params.toString());
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : params.keySet()) {
            builder.add(key, params.get(key));
        }
        FormBody body = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }
}
